/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.peng.service.impl;

import me.peng.domain.RecyclerCredits;
import me.peng.utils.ValidationUtil;
import me.peng.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.peng.repository.RecyclerCreditsRepository;
import me.peng.service.RecyclerCreditsService;
import me.peng.service.dto.RecyclerCreditsDto;
import me.peng.service.dto.RecyclerCreditsQueryCriteria;
import me.peng.service.mapstruct.RecyclerCreditsMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.peng.utils.PageUtil;
import me.peng.utils.QueryHelp;

import java.util.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author pengjianye
* @date 2021-09-30
**/
@Service
@RequiredArgsConstructor
public class RecyclerCreditsServiceImpl implements RecyclerCreditsService {

    private final RecyclerCreditsRepository recyclerCreditsRepository;
    private final RecyclerCreditsMapper recyclerCreditsMapper;



    @Override
    public Map<String,Object> queryAll(RecyclerCreditsQueryCriteria criteria, Pageable pageable){
        Page<RecyclerCredits> page = recyclerCreditsRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(recyclerCreditsMapper::toDto));
    }

    @Override
    public List<RecyclerCreditsDto> queryAll(RecyclerCreditsQueryCriteria criteria){
        return recyclerCreditsMapper.toDto(recyclerCreditsRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public RecyclerCreditsDto findById(Integer id) {
        RecyclerCredits recyclerCredits = recyclerCreditsRepository.findById(id).orElseGet(RecyclerCredits::new);
        ValidationUtil.isNull(recyclerCredits.getId(),"RecyclerCredits","id",id);
        return recyclerCreditsMapper.toDto(recyclerCredits);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RecyclerCreditsDto create(RecyclerCredits resources) {
        return recyclerCreditsMapper.toDto(recyclerCreditsRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(RecyclerCredits resources) {
        RecyclerCredits recyclerCredits = recyclerCreditsRepository.findById(resources.getId()).orElseGet(RecyclerCredits::new);
        ValidationUtil.isNull( recyclerCredits.getId(),"RecyclerCredits","id",resources.getId());
        recyclerCredits.copy(resources);
        recyclerCreditsRepository.save(recyclerCredits);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            Optional<RecyclerCredits> resul=recyclerCreditsRepository.findById(id);
            //传递url地址数据,循环遍历去传递文件url地址进行删除
            resul.map(RecyclerCredits::getPath).ifPresent(FileServiceImpl::deleteFileData);
            recyclerCreditsRepository.deleteById(id);

        }

    }



    @Override
    public void download(List<RecyclerCreditsDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (RecyclerCreditsDto recyclerCredits : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("图片地址", recyclerCredits.getPath());
            map.put("标题", recyclerCredits.getTitle());
            map.put("积分", recyclerCredits.getCreditsnum());
            map.put("信息", recyclerCredits.getInfo());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
